<?php
/**
* Notes: domdocument::domdocument error/warning about 1 param, not zero; solved by disabling domxml in php config
*/
class Events extends Controller{
    public function __construct() {
            parent::Controller();
            $this->load->model("EventsModel");
            $this->load->scaffolding("events");
            $this->load->helper("comfunc");
            $this->load->helper("url");
        }
    /*********** URL Views ***************/
    public function index() {
            $headerData = array(
                "pageTitle"=>"Events | Washington Homebrewers Association",
                "pageStyleIndex"=>1, /* Header/Background/Logo pairing */
                "styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/calendar_style.css\" />"),
                "scriptIncludes"=>array("<script type=\"text/javascript\" src=\"/scripts/calendar.js\"></script>"),
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $pageData = array();
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/index.php",$pageData);
            $this->load->view("PageTemplate_footer.php",$footerData);
        }
    public function missing() {
            $headerData = array(
                "pageTitle"=>"Error:Event Not Found | Washington Homebrewers Association",
                "pageStyleIndex"=>1, /* Header/Background/Logo pairing */
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $pageData = array();
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/event_not_found.php",$pageData);
            $this->load->view("PageTemplate_footer.php",$footerData);
        }
    public function view() {
            $this->load->helper('url');
            $headerData = array(
                "pageTitle"=>"Events | Washington Homebrewers Association",
                "pageStyleIndex"=>1, /* Header/Background/Logo pairing */
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $pageData = array("event"=>$this->EventsModel->getSingleEvent($this->uri->segment(3)),
                "col1"=>array("articles/events/view/00001.html","articles/events/view/00002.html"),
                "col3"=>array("articles/events/view/00002.html"));
            if(count($pageData["event"])<=0){
                    redirect("events/missing");
                    return;
                }
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/view.php",$pageData);
            $this->load->view("PageTemplate_footer.php",$footerData);
        }
    public function remove() {
            $this->load->helper('url');
            $headerData = array(
                "pageTitle"=>"Remove Event | Washington Homebrewers Association",
                "pageStyleIndex"=>1, /* Header/Background/Logo pairing */
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $pageData = array("event"=>$this->EventsModel->getSingleEvent($this->uri->segment(3)),
                "col1"=>array("articles/events/view/00001.html","articles/events/view/00002.html"),
                "col3"=>array("articles/events/view/00002.html"),
                "handleURL"=>"/events/handleDelete",
                "submitTxt"=>"Remove Event?"
                );
            if(count($pageData["event"])<=0){
                    redirect("events/missing");
                    return;
                }
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/remove.php",$pageData);
            $this->load->view("PageTemplate_footer.php",$footerData);
        }
    public function create() {
            $headerData = array(
                "pageTitle"=>"Events | Washington Homebrewers Association",
                "pageStyleIndex"=>0, /* Header/Background/Logo pairing */
                "styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/calendar_style.css\" />","<link href=\"/styles/iDMSCalendar.css\" type=\"text/css\" rel=\"stylesheet\" />","<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/form.css\" />"),
                "scriptIncludes"=>array("<script type=\"text/javascript\" src=\"/scripts/calendar.js\"></script>","<script type=\"text/javascript\" src=\"/scripts/iDMSCalendar.js\"></script>"),
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $pageData=array("subtitle"=>"Create an Event",
                "handleURL"=>"/events/handleEvent",
                "submitTxt"=>"Create Event"
                );
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/create.php",$pageData);
            $this->load->view("PageTemplate_footer.php");
        }
    public function modify() {
            $headerData = array(
                "pageTitle"=>"Events | Washington Homebrewers Association",
                "pageStyleIndex"=>0, /* Header/Background/Logo pairing */
                "styleIncludes"=>array("<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/calendar_style.css\" />","<link href=\"/styles/iDMSCalendar.css\" type=\"text/css\" rel=\"stylesheet\" />","<link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/form.css\" />"),
                "scriptIncludes"=>array("<script type=\"text/javascript\" src=\"/scripts/calendar.js\"></script>","<script type=\"text/javascript\" src=\"/scripts/iDMSCalendar.js\"></script>"),
                "selectedNav"=>"1" /* Selected top level nav */
                );
            $default_values = $this->EventsModel->getSingleEvent($this->uri->segment(3));
            if(count($default_values)<=0){
                    redirect("events/missing");
                    return;
                }
                //[id][title][startDate][startTime][endDate][endTime][short_description][long_description][locArr][contactArr][web]
            $pageData=array("default_values"=>$default_values[0],
                "subtitle"=>"Modify an Event",
                "handleURL"=>"/events/handleUpdate",
                "submitTxt"=>"Update Event"
                );
            $footerData = array();
            $this->load->view("PageTemplate_header.php",$headerData);
            $this->load->view("events/create.php",$pageData);
            $this->load->view("PageTemplate_footer.php");
        }
    /********** Form Handlers ***************/
    public function handleEvent() {
            $title = $_POST["event_name"];
            $desc = $_POST["short_description"];
            $ldesc = $_POST["long_description"];
            $sDate = $_POST["sdate_val"];
            $eDate = $_POST["edate_val"];
            $web = $_POST["web_link"];
                //$calUserName='default',$title,$desc="",$startDate,$startTime="",$endDate="",$endTime="",$where="",$tzOffset="-07"
            $event = $this->EventsModel->createEvent($title,$sDate,$eDate,$desc,$ldesc,$web);
            redirect("/events/");
        }
    public function handleUpdate() {
            $eventId = $_POST["event_id"];
            $title = $_POST["event_name"];
            $desc = $_POST["short_description"];
            $ldesc = $_POST["long_description"];
            $sDate = $_POST["sdate_val"];
            $eDate = $_POST["edate_val"];
            $web = $_POST["web_link"];
            $event = $this->EventsModel->updateEvent($eventId,$title,$sDate,$eDate,$desc,$ldesc,$web);
            if($event)
                    redirect("/events/");
                else
                    echo "<h2>ERROR WHILE UPDATING EVENT</h2>";
        }
    public function handleDelete() {
            $eventId = $_POST["event_id"];
            $event = $this->EventsModel->deleteEvent($eventId);
            if($event)
                redirect("/events/");
            else
                echo "<h2>ERROR REMOVING EVENT</h2>";
        }
}
?>